﻿using System.Windows;
using System.Windows.Controls;
using Aeon.Configuration;
using Microsoft.Win32;

namespace Aeon.DiskImages.Views
{
    /// <summary>
    /// Displays the UI for configuring a disk image file.
    /// </summary>
    [DriveConfigurationView(typeof(ISOImageConfiguration))]
    public partial class DiskImageView : UserControl, IDriveConfigurationView
    {
        /// <summary>
        /// DriveConfiguration dependency property.
        /// </summary>
        public static readonly DependencyProperty DriveConfigurationProperty = DependencyProperty.Register("DriveConfiguration", typeof(DiskImageConfiguration), typeof(DiskImageView), new PropertyMetadata(null));

        /// <summary>
        /// Initializes a new instance of the DiskImageView class.
        /// </summary>
        public DiskImageView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the drive configuration being viewed. This is a dependency property.
        /// </summary>
        public DiskImageConfiguration DriveConfiguration
        {
            get { return (DiskImageConfiguration)GetValue(DriveConfigurationProperty); }
            set { SetValue(DriveConfigurationProperty, value); }
        }

        /// <summary>
        /// Gets or sets the drive configuration being viewed.
        /// </summary>
        IDriveConfiguration IDriveConfigurationView.DriveConfiguration
        {
            get { return this.DriveConfiguration; }
            set { this.DriveConfiguration = (DiskImageConfiguration)value; }
        }

        /// <summary>
        /// Invoked when the Browse button is clicked.
        /// </summary>
        /// <param name="source">Source of the event.</param>
        /// <param name="e">Information about the event.</param>
        private void BrowseButton_Click(object source, RoutedEventArgs e)
        {
            if(this.DriveConfiguration != null)
            {
                var dialog = new OpenFileDialog()
                {
                    Title = "Select disk image file...",
                    Filter = "Supported image files|" + this.DriveConfiguration.FileExtensions
                };

                if(dialog.ShowDialog(Window.GetWindow(this)) == true)
                    this.DriveConfiguration.ImageFilePath = dialog.FileName;
            }
        }
    }
}
